//
//  LSEmoticonPackages.swift
//  EmotionKeyboard
//
//  Created by lj on 15/9/12.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class LSEmoticonPackages: NSObject {
    
    /// 目录名
    var id:String?
    /// 分组名
    var group_name_cn: String?
    /// 表情数组
    var emoticons = [LSEmoticon]()
    
    init(dict:[String: AnyObject]) {
        super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        var index = 0
        if let array = dict["emoticons"] as? [[String: String]] {
            
            for var emoticonDict in array {
                // 判断字典中是否包含 png 的 key，排除 emoji 没有图片的情况
                if let imagePath = emoticonDict["png"] {
                
                    emoticonDict["png"] = id! + "/" + imagePath
                }
                emoticons.append(LSEmoticon(dict: emoticonDict))
                
                index++
                if index == 20 {
                    emoticons.append(LSEmoticon(isRemove: true))
                    index = 0
                }
                
            }
        }
        appendBlankEmoticon()
    }
    
    /// 追加空白表情
    func appendBlankEmoticon() {
        let count = emoticons.count % 21
        // 如果刚好被 21 整除直接返回
        if count == 0 && emoticons.count > 0  {
            return
        }
        
        // 如果有需要补足的情况，表情数组完全为空
        // 追加到20个空白按钮
        for _ in count..<20 {
            emoticons.append(LSEmoticon(isEmpty: true))
        }
        
        // 末尾追加一个删除按钮
        emoticons.append(LSEmoticon(isRemove: true))
    }
    
    
    override var description:String {
        
        let key = ["id","group_name_cn","emoticons"]
        
        return dictionaryWithValuesForKeys(key).description
    }

}
